package com.light.roller;


import java.util.ArrayList;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;

import com.badlogic.gdx.math.Vector2;
import com.huw.cameramanagers.CameraManager;
import com.huw.cameramanagers.CameraManager.FollowStatus;
import com.light.buttons.Button;
import com.light.gamecomponents.IGameComponents;
import com.light.gamecomponents.gravitylight.GravityLight;
import com.light.gamecomponents.physics.MainPieces;
import com.light.gamecomponents.physics.MovingBar;
import com.light.gamecomponents.physics.PhysicsBox;
import com.light.gamecomponents.physics.PhysicsChain;
import com.light.gamecomponents.physics.PhysicsComponents;
import com.light.gamecomponents.physics.RollerPhysicsWorld;
import com.light.mapcomponents.GameMap;

public class Stage implements IUpdateHandler, IGameComponents {
	
	public enum StageState { BLACKSCREEN, PREPARE, PLAYING, FINISH };
	
	
	protected Roller m_game;

	protected GameMap m_map;
	protected ArrayList<PhysicsComponents> m_componentsList = new ArrayList<PhysicsComponents>();
	protected ArrayList<GravityLight> lightList = new ArrayList<GravityLight>();
	protected StageState m_state = StageState.PREPARE;
	private float m_timer = 0;
	public StageState getState() { return m_state; }
	public void addGravityLight(GravityLight light) { lightList.add(light); }
	public void removeGravityLight(GravityLight light) { lightList.remove(light); }
	
	private PhysicsChain tempChain ;
	private MovingBar tempBar;
	
	public GameMap getGameMap() { return m_map; }
	
	public void removePhysicComponent(PhysicsComponents pc)
	{
		if (m_componentsList.contains(pc))
			m_componentsList.remove(pc);
	}
	
	public Stage(Roller game) {
		this.m_game = game;
	}
	
	public void loadStage() {
		m_map = new GameMap(m_game);
		m_map.loadMap();
		
	}
	
	
	protected MainPieces mainPieces[] = new MainPieces[7];
	
	protected void initializePieces()
	{		
		mainPieces[0]=new MainPieces(m_game, "mainHead", new Vector2(MainPieces.INITX, MainPieces.INITY), Roller.mainHeadSize);
		mainPieces[1]=new MainPieces(m_game, "mainBody1", new Vector2(MainPieces.INITX, MainPieces.INITY), Roller.mainBodySize);
		mainPieces[2]=new MainPieces(m_game, "mainBody2", new Vector2(MainPieces.INITX, MainPieces.INITY), Roller.mainBodySize);
		mainPieces[3]=new MainPieces(m_game, "mainHand1", new Vector2(MainPieces.INITX, MainPieces.INITY), Roller.mainHand1Size);
		mainPieces[4]=new MainPieces(m_game, "mainHand2", new Vector2(MainPieces.INITX, MainPieces.INITY), Roller.mainHand2Size);
		mainPieces[5]=new MainPieces(m_game, "mainLeg1", new Vector2(MainPieces.INITX, MainPieces.INITY), Roller.mainLeg1Size);
		mainPieces[6]=new MainPieces(m_game, "mainLeg2", new Vector2(MainPieces.INITX, MainPieces.INITY), Roller.mainLeg2Size);	
		
		for(int i=0;i<mainPieces.length;i++)
		{
			this.m_componentsList.add(mainPieces[i]);
		}
	}
	
	@Override
	public void onLoadEngine() {
		loadStage();
		m_map.onLoadEngine();
		for (int i = 0; i < m_componentsList.size(); i++)
			m_componentsList.get(i).onLoadEngine();
	}
	@Override
	public void onLoadResources() {
		
		m_map.onLoadResources();
		for (int i = 0; i < m_componentsList.size(); i++)
			m_componentsList.get(i).onLoadResources();
	}
	
	@Override
	public void unLoadResources() 
	{
		for (int i = 0; i < m_componentsList.size(); i++)
			m_componentsList.get(i).unLoadResources();
		
	}
	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWorld) 
	{
		m_map.onLoadScene(scene, physicsWorld);
		
		
		for (int i = 0; i < m_componentsList.size(); i++)
			m_componentsList.get(i).onLoadScene(scene, physicsWorld);
		
		//tempChain.onLoadScene(scene, physicsWorld);//ATTENTION!! This statement must behind the for loop!!
		((RollerPhysicsWorld)physicsWorld).registerUpdateHandler(this);
		
	}
	
	public void removeStoper(Scene scene, PhysicsWorld physicsWorld) 
	{
		m_map.removeStoper(scene, physicsWorld);

	}
	private void checkLightAndPhysicsComponents()
	{
		for (int i = 0; i < m_componentsList.size(); i++)
		{
			boolean collided = false;
			for (int j = 0; j < lightList.size(); j++)
			{
				if (lightList.get(j).collidesWith(m_componentsList.get(i)))
				{
					collided = true;
					break;
				}
				//collided = true;
			}
			if (collided == true)
				m_componentsList.get(i).startBody();
			else
				m_componentsList.get(i).stopBody();
		}
		
		for (int i = 0; i < m_map.getPanelComponentsList().size(); i++)
		{
			boolean collided = false;
			for (int j = 0; j < lightList.size(); j++)
			{
				if (lightList.get(j).collidesWith(m_map.getPanelComponentsList().get(i)))
				{
					collided = true;
					break;
				}
				//collided = true;
			}
			if (collided == true)
				m_map.getPanelComponentsList().get(i).startAnimate();
			else
				m_map.getPanelComponentsList().get(i).stopAnimate();
		}
	}
	
	@Override
	public void onUpdate(float pSecondsElapsed) {
		switch (m_state)
		{
		case PREPARE:
			if (Roller.cameraManager.followStatus == FollowStatus.Gaming)
				Roller.exitButton.changeToMenu();
			break;
		case PLAYING:

		}
		checkLightAndPhysicsComponents();
	}
	@Override
	public void reset() {
		// TODO Auto-generated method stub
	}
	
	
	public void endPrepare()
	{
		Roller.jumpButton.setState(Button.ButtonState.CONTROLABLE);
		Roller.jumpButton.showButton();
		Roller.actionButton.setState(Button.ButtonState.CONTROLABLE);
		Roller.actionButton.showButton();
		Roller.rightButton.setState(Button.ButtonState.CONTROLABLE);
		Roller.rightButton.showButton();
		Roller.leftButton.setState(Button.ButtonState.CONTROLABLE);
		Roller.leftButton.showButton();
		this.m_state = StageState.PLAYING;
		m_game.initializeDirection();
	}
	
	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) {
		// TODO Auto-generated method stub
		
	}
	public void setStageState(StageState stageState)
	{
		m_state=stageState;
	}
	
}
